/* eslint-disable no-param-reassign */
import menuApi from '@/api/modules/system/menu'
function getMenuData () {
  //cjf 221021 无更新机制，暂不从缓存读取
  // const data = sessionStorage.getItem('menuData')
  // if (data) {
  //   return JSON.parse(data)
  // }
  return []
}
const state = {
  menuData: getMenuData()
}
const mutations = {
  SET_MENU_DATA (state, val) {
    state.menuData = val
    sessionStorage.setItem('menuData', JSON.stringify(val))
  }
}

const actions = {
  getMenuData ({ commit }) {
    return new Promise((resolve, reject) => {
      menuApi.getMenuAllData().then(res => {
        commit('SET_MENU_DATA', res.data)
        resolve(true)
      }).catch(err => {
        console.log(err)
        reject(err)
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
